import sys
sys.path.append("../")
from frameworks.cores.LoadClass import *
from frameworks.utils.RedisUtil import *
from services.CodesService import *
from frameworks.utils.MyThread import *
import json

class PushStockController:
    def __init__(self):
        self.redis = RedisUtil()
        self.load = LoadClass()

    def multirun(self, args={}):
        for i in range(0,len(args["codes"])):
            code = args["codes"][i]
            try:
                self.pushStockData(code)
            except:
                print("===="+ code +"没有成功===")

    def pushStockData(self,code):
        info = {}

        args = {"code": code}

        args["func"] = "getCodeInfo"
        # 获取个股公司信息
        rs = self.load.load("controller.CodeBaseController", "CodeBaseController", args)
        if len(rs) > 0:
            info["baseinfo"] = rs[0]
        else:
            info["baseinfo"] = []

        args["func"] = "getCompanyInfo"
        # 获取个股公司信息
        rs = self.load.load("controller.CompanyController", "CompanyController", args)

        if len(rs) > 0:
            info["code"] = code
            info["corp_name"] = rs[0]["corp_name"]
            info["person"] = rs[0]["representative"]
            info["address"] = rs[0]["registry_address"]
            info["corp_summary"] = rs[0]["corp_summary"]
            info["business_range"] = rs[0]["business_range"]
        else:
            info["code"] = code
            info["corp_name"] = ""
            info["person"] = ""
            info["address"] = ""
            info["corp_summary"] = ""
            info["business_range"] = ""

        args["func"] = "getCompanyPersonInfo"
        # 获取个股股东信息
        rs = self.load.load("controller.CompanyPersonController", "CompanyPersonController", args)
        personinfo = []
        allmoney = 0
        if "all_money" in info["baseinfo"]:
            allmoney = round(info["baseinfo"]["all_money"] / 100000000, 2)
        print(allmoney)

        if len(rs) > 0:
            for row in json.loads(rs[0]["person_summary"]):
                radio = float(row["hand"].replace("%", "").replace("↑", "").replace("↓", ""))
                personinfo.append({"gudong": row["gudong"], "hand": row["hand"],
                                   "money": round(allmoney * radio / 100, 2)})
        else:
            for i in range(10):
                personinfo.append({"gudong": "", "hand": "", "money": ""})
        info["company_person"] = personinfo

        args["func"] = "getCompanyNewsInfo"
        # 获取个股股东信息
        rs = self.load.load("controller.CompanyNewsController", "CompanyNewsController", args)

        newsobj = json.loads(rs[0]["news"])
        if "Data" in newsobj:
            info["news"] = newsobj["Data"]
        else:
            info["news"] = []

        noticesobj = json.loads(rs[0]["notices"])
        if "data" in noticesobj:
            info["notices"] = noticesobj["data"]
        else:
            info["notices"] = []
        print(rs[0].keys())

        reportsobj = json.loads(rs[0]["reports"])
        if len(reportsobj) > 0:
            info["reports"] = reportsobj
        else:
            info["reports"] = []
        print(info)
        self.redis.setArray("allinfo_"+code,[info])

    def getStockData(self,code):
        data = self.redis.getArray("allinfo_"+code)
        for line in data:
            obj = json.loads(line.replace("'","\""))
            return obj
